﻿
    using Chain.BLL;
    using System;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class coupon : Page
    {

        private Coupon CouponBll = new Coupon();
        private CouponList CouponListBll = new CouponList();
        protected int MemID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((base.Request.Cookies["uid"] != null) && Regex.IsMatch(base.Request.Cookies["uid"].Value, @"^\d+$"))
            {
                this.MemID = Convert.ToInt32(base.Request.Cookies["uid"].Value);
            }
            DataTable dt = this.CouponBll.GetList("(CouponEffective=0 or (CouponEffective=1 and CouponEnd >= getDate())) order by ID desc").Tables[0];
            this.rptCoupon.DataSource = dt;
            this.rptCoupon.DataBind();
            if (dt.Rows.Count == 0)
            {
                this.msg.Style.Add("display", "");
            }
        }

        protected void rptCoupon_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                DataRowView row = (DataRowView) e.Item.DataItem;
                int CouponType = Convert.ToInt32(row["CouponType"]);
                decimal CouponMinMoney = Convert.ToDecimal(row["CouponMinMoney"]);
                int CouponNumber = Convert.ToInt32(row["CouponNumber"]);
                string CouponStart = row["CouponStart"].ToString();
                string CouponEnd = row["CouponEnd"].ToString();
                int CouponPredictNu = Convert.ToInt32(row["CouponPredictNu"]);
                int CouponYF = Convert.ToInt32(row["CouponYF"]);
                int CouponEffective = Convert.ToInt32(row["CouponEffective"]);
                HtmlGenericControl spCouponDesc = (HtmlGenericControl) e.Item.FindControl("spCouponDesc");
                spCouponDesc.InnerText = (CouponType == 0) ? string.Format("最低消费￥{0}可优惠￥{1}", CouponMinMoney.ToString("0.00"), CouponNumber.ToString("0.00")) : string.Format("最低消费￥{0}可打{1}折", CouponMinMoney.ToString("0.00"), CouponNumber.ToString("0.0"));
                HtmlGenericControl spCouponTime = (HtmlGenericControl) e.Item.FindControl("spCouponTime");
                spCouponTime.InnerText = (CouponEffective == 0) ? "永久有效" : string.Format("{0}至{1}", Convert.ToDateTime(CouponStart).ToString("yyyy-MM-dd"), Convert.ToDateTime(CouponEnd).ToString("yyyy-MM-dd"));
                int CouPonID = Convert.ToInt32(row["ID"]);
                string strWhere = string.Format("CouPonMID={0} and CouPonID={1} and CouPonSY=0", this.MemID, CouPonID);
                DataTable tempDT = this.CouponListBll.GetList(1, strWhere, "CID").Tables[0];
                HtmlGenericControl pCouponNumber = (HtmlGenericControl) e.Item.FindControl("pCouponNumber");
                HtmlAnchor aCouponClick = (HtmlAnchor) e.Item.FindControl("aCouponClick");
                if (tempDT.Rows.Count > 0)
                {
                    pCouponNumber.InnerHtml = "优惠券号：<span style=\"color:Red\">" + tempDT.Rows[0]["CouPon"] + "</span>";
                    aCouponClick.Attributes.Add("data-icon", "back");
                    aCouponClick.Attributes.Add("href", "#index");
                }
                else if (CouponYF >= CouponPredictNu)
                {
                    pCouponNumber.InnerHtml = "该优惠券已发完";
                    aCouponClick.Attributes.Add("data-icon", "back");
                    aCouponClick.Attributes.Add("href", "#index");
                }
                else
                {
                    pCouponNumber.InnerHtml = "点击右侧图标申请";
                    aCouponClick.Attributes.Add("data-icon", "star");
                    aCouponClick.Attributes.Add("Coupon", CouPonID.ToString());
                }
            }
        }
    }


